#include "basewindow.h"
#include<QGridLayout>
#include<QVBoxLayout>
#include<QDebug>

basewindow::basewindow(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground,true);
    setMouseTracking(true);
    setMinimumSize(1000,650);
    m_border = 9;
    m_bgcolor = QColor(22, 154, 218);
    //中央区域
    m_mainwid=new QWidget(this);
    m_mainwid->setProperty("QQMainWindow", QVariant(true));
    m_mainwid->setStyleSheet("QWidget[QQMainWindow=true] {background-color:#ffffff;}");

    QGridLayout *lyout=new QGridLayout;
    lyout->addWidget(m_mainwid);
    lyout->setContentsMargins(4,4,4,4);
    setLayout(lyout);
}

basewindow::~basewindow()
{

}

void basewindow::setBgcolor(QColor bgcolor)
{
    m_bgcolor = bgcolor;
}

void basewindow::initBackGroundColor()
{
    //从QWidget子类化一个Widget，则需要在paintEvent提供以下代码才能使用样式表
    //设置背景色，执行此代码QT样式才能生效
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void basewindow::paintEvent(QPaintEvent *e)
{
    QPainterPath path;
    path.setFillRule(Qt::WindingFill);
    path.addRect(9, 9, this->width()-18, this->height()-18);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.fillPath(path, Qt::transparent);
    QColor color(0, 0, 0, 50);
    for(int i=0; i<9; i++)
    {
        QPainterPath path;
        path.setFillRule(Qt::WindingFill);
        path.addRect(9-i, 9-i, this->width()-(9-i)*2, this->height()-(9-i)*2);
        color.setAlpha(150 - qSqrt(i)*50);
        painter.setPen(color);
        painter.drawPath(path);
    }
    initBackGroundColor();
    QWidget::paintEvent(e);
}

////////////////////////////////////////////////////////////////////
basedialog::basedialog(QWidget *parent) : QDialog(parent)
{
    setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::X11BypassWindowManagerHint);
    setAttribute(Qt::WA_TranslucentBackground,true);
    setMouseTracking(true);
    resize(600,400);
    m_border = 9;
    m_state.MousePressed = false;
    m_bgcolor = QColor(22, 154, 218);
    initControl();
    setLayoutWidget();
}

basedialog::~basedialog()
{

}

void basedialog::initControl()
{
    //中央区域
    m_mainwid=new QWidget(this);
    m_mainwid->setProperty("QQMainDialog", QVariant(true));
    m_mainwid->setStyleSheet("QWidget[QQMainDialog=true] {background-color:#ffffff;}");

    m_topwid = new QWidget(m_mainwid);
    m_topwid->setFixedHeight(30);
    m_topwid->setProperty("topwid", QVariant(true));
    QString str_topcolor = QString("QWidget[topwid=true] {background-color:rgb(%1,%2,%3);}"
                                   ).arg(m_bgcolor.red()).arg(m_bgcolor.green()).arg(m_bgcolor.blue());
    m_topwid->setStyleSheet(str_topcolor);
    m_contentwid = new QWidget(m_mainwid);
}

void basedialog::setBgcolor(QColor bgcolor)
{
    m_bgcolor = bgcolor;
}

void basedialog::setLayoutWidget()
{
    QVBoxLayout *contentlayout = new QVBoxLayout(m_mainwid);
    contentlayout->addWidget(m_topwid);
    contentlayout->addWidget(m_contentwid);
    contentlayout->setSpacing(0);
    contentlayout->setMargin(0);
    m_mainwid->setLayout(contentlayout);

    QGridLayout *lyout=new QGridLayout;
    lyout->addWidget(m_mainwid);
    lyout->setContentsMargins(4,4,4,4);
    setLayout(lyout);
}

void basedialog::initBackGroundColor()
{
    //从QWidget子类化一个Widget，则需要在paintEvent提供以下代码才能使用样式表
    //设置背景色，执行此代码QT样式才能生效
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void basedialog::paintEvent(QPaintEvent *e)
{
    QPainterPath path;
    path.setFillRule(Qt::WindingFill);
    path.addRect(9, 9, this->width()-18, this->height()-18);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.fillPath(path, Qt::transparent);

    QColor color(0, 0, 0, 50);
    for(int i=0; i<9; i++)
    {
        QPainterPath path;
        path.setFillRule(Qt::WindingFill);
        path.addRect(9-i, 9-i, this->width()-(9-i)*2, this->height()-(9-i)*2);
        color.setAlpha(150 - qSqrt(i)*50);
        painter.setPen(color);
        painter.drawPath(path);
    }
    initBackGroundColor();
    QDialog::paintEvent(e);
}

void basedialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_state.WindowPos = this->pos();
        if(QRect(m_border+1,m_border+1,width()-(m_border+1)*2,height()-(m_border+1)*2).contains(QPoint(event->globalX()-x(),event->globalY()-y())))
        {
            m_state.MousePos = event->globalPos();
            m_state.MousePressed = true;
        }
    }
}

void basedialog::mouseMoveEvent(QMouseEvent *event)
{
    if (m_state.MousePressed)
    {
        this->move(m_state.WindowPos + (event->globalPos() - m_state.MousePos));
    }
}

void basedialog::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        this->m_state.MousePressed = false;
    }
}








